package com.yilin.medical.lsh;


import java.lang.reflect.Type;

import com.google.gson.internal.$Gson$Types;

public class SimpleException extends Exception {
	/**
	 */
	private static final long serialVersionUID = 1L;

	public SimpleException() {
		super();
	}

	public SimpleException(String msg) {
		super(msg);
	}
	
	public static SimpleException newInstance(String msg, Throwable cause){
		StackTraceElement element = cause.getStackTrace()[1];
		String className = element.getClassName();
		className = className.substring(className.lastIndexOf(".") + 1);
		String methodName = element.getMethodName(); 
		msg = "--"+className+"----" + methodName +"----:" + msg;
		return new SimpleException(msg,cause);
	}
	
	public static SimpleException newInstance(String msg){
		Exception exception = new Exception();
		return SimpleException.newInstance(msg, exception);
	}
	
	public  BaseBean instance(){
		BaseBean b = new BaseBean();
		b.setSimpleException(this);
		return b;
	}
	
	SimpleException(String msg, Throwable cause) {
		super(msg, cause);
	}

	public SimpleException(Throwable cause) {
		super(cause);
	}
	
	
	@Override
	public String getMessage() {
		// TODO Auto-generated method stub
		String message = super.getMessage();
		String[] split = message.split(":");
		if(split.length >= 2){
			message = split[1];
		}
		return message;
		
	}
	
	public <T extends BaseBean> T convertToBean(Class<T> clazz){
		T t = null;
		try {
			t = clazz.newInstance();
			t.setSimpleException(this);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return t;
	}
	
	public BaseBean convertToBean(Type type){
		BaseBean t = null;
		Logs.i("exception", type.toString());

//        String sdf = null;
//        switch (sdf){
//            case "sdf":
//                break;
//        }

		try {
			Class<?> clazz = $Gson$Types.getRawType(type);
			t = (BaseBean) clazz.newInstance();
			t.setSimpleException(this);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} 
		return t;
	}
	
	
}
